package cn.gaozhong.basic.controller;


import cn.gaozhong.basic.dto.RegisterDto;
import cn.gaozhong.basic.exception.CustomException;
import cn.gaozhong.basic.service.IVerifyCodeService;
import cn.gaozhong.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;



    @PostMapping("/sendSmsCode")
    public AjaxResult sendSmsCode(@RequestBody RegisterDto dto){
        try {
            verifyCodeService.sendSmsCode(dto);
            return AjaxResult.me();
        } catch (CustomException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }

    }

    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult imgCode(@PathVariable("imageCodeKey") String key){
        try {
            String base64str=verifyCodeService.imgCode(key);
            return AjaxResult.me().setResultObj(base64str);
        } catch (CustomException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }

    }
}
